package com.example.demo.controller;
import com.example.demo.Course.Course;
import com.example.demo.Course.CourseList;
import com.example.demo.User.User;
import com.example.demo.User.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Controller
public class LoginController {
    private CourseList list=new CourseList();
    private UserService userService;
    private User user;

    @Autowired
    public LoginController(UserService userService) {
        this.userService = userService;
    }
    public void LoginUser(User user) {
        this.user = user;
    }
    @RequestMapping("/login")
    public String showLoginPage() {
        return "login"; // 返回 login.html 页面
    }

    @PostMapping("/login")
    public String handleLogin(
            @RequestParam String adminId,
            @RequestParam String password,
            Model model) {
        User user = userService.authenticate(adminId, password);
        if (user!=null){
            LoginUser(user);
            model.addAttribute("username", user.getName());
            model.addAttribute("identity", user.getIdentity());
            return "dashboard"; // 登录成功跳转到仪表盘
        } else {
            model.addAttribute("error", "登录失败，管理员ID或密码错误！");
            return "login"; // 登录失败返回登录页面
        }
    }
    //选课
//选课
    @GetMapping("/choosecourse")
    public String choosecourse(Model model) {
        List<Course> allCourses = list.getCourses();
        List<Course> availableCourses = new ArrayList<>();

        if (user != null) {
            List<Course> selectedCourses = user.getSelectedCourses();
            model.addAttribute("selectedCourses", selectedCourses);

            // 过滤掉已选课程
            for (Course course : allCourses) {
                boolean isSelected = false;
                for (Course selected : selectedCourses) {
                    if (course.getId() == selected.getId()) {
                        isSelected = true;
                        break;
                    }
                }
                if (!isSelected) {
                    availableCourses.add(course);
                }
            }
        } else {
            availableCourses = allCourses;
        }

        model.addAttribute("courselist", availableCourses);
        return "choosecourse";
    }
    @PostMapping("/choosecourse")
    public String handleChoose(@RequestParam int courseId, @RequestParam String courseName, Model model) {
        // 从现有课程列表中查找对应的课程
        Course selectedCourse = null;
        for (Course course : list.getCourses()) {
            if (course.getId() == courseId) {
                selectedCourse = course;
                break;
            }
        }

        if (selectedCourse != null && user.addcourse(selectedCourse)) {
            return "redirect:/choosecourse"; // 重定向回选课页面，显示更新后的已选课程
        } else {
            model.addAttribute("error", "添加失败");
            return "choosecourse";
        }
    }

    // 退出登录，清除 session 信息
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate(); // 清除所有 session 信息
        return "redirect:/login"; // 退出后重定向到登录页面
    }
    @GetMapping("/dashboard")
    public String dashboard(Model model){
        if (user!=null){
            model.addAttribute("username", user.getName());
            model.addAttribute("identity", user.getIdentity());
            return "dashboard"; // 登录成功跳转到仪表盘
        }
        else return "login";
    }


}
